package com.cdc.views;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
/**
 * Created by admin on 2018/3/21.
 */

public class RoundPictureView extends AppCompatImageView {

    private Paint mPaint;
    //画笔过滤模式，显示下层
    private Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

    //这个控件的宽和高
    private int mHeight;
    private int mWidth;

    public RoundPictureView(Context context) {
        this(context, null);
    }

    public RoundPictureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundPictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {
        //初始化画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //得到宽和高，且必须相等
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        if(mWidth != mHeight){
            throw new IllegalArgumentException("width and height of view required equals");
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //保存图层，不知道为什么要这一步，不加就错
//        canvas.saveLayer(0, 0, mWidth, mHeight,Canvas.MATRIX_SAVE_FLAG |Canvas.CLIP_SAVE_FLAG |
//                        Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
//                        Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
//                        Canvas.CLIP_TO_LAYER_SAVE_FLAG);

        //得到bitmap
        Bitmap bitmap = drawable2Bitmap(getDrawable());
        //把图片缩放到合适的比例
        float xScale = (float) (mWidth*1.0/bitmap.getWidth());
        float yScale = (float) (mHeight*1.0/bitmap.getHeight());
        Matrix matrix = new Matrix();
        matrix.setScale(xScale, yScale);
        //画图片
        canvas.drawBitmap(bitmap, matrix, mPaint);
        //设置过滤模式
        mPaint.setXfermode(xfermode);
        //画圆
        canvas.drawBitmap(getRoundBitmap(), 0,0, mPaint);
        //必须的一步，去掉过滤模式，否则图片显示不出来
        mPaint.setXfermode(null);

    }

    /**
     * drawable 转 bitmap
     */
    private Bitmap drawable2Bitmap(Drawable drawable){

        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    /**
     * 画一个圆形的bitmap
     */
    private Bitmap getRoundBitmap(){
        Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(mWidth/2, mHeight/2, mWidth/2, paint);

        return bitmap;
    }
}
